﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YYTmEditer.MtObject;
using yyycommon;

namespace YYTmEditer
{
    /// <summary>
    /// UnitControll.xaml 的交互逻辑
    /// </summary>
    public partial class UnitControll : UserControl
    {
        public UnitControll()
        {
            InitializeComponent();
        }

        private List<Card> cards;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cards = GameObject.Instance.GetCards();
            reload();
        }

        private void reload() {
            this.listBox.ItemsSource = null;
            this.listBox.ItemsSource = cards;
            this.listBox.SelectedIndex = 0;
        }

        private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            var select = listBox.SelectedItem as Card;
            if (select != null)
            {
                var be = new BlessingEditer();
                var eo = new EditObject(select.ToString(),select.cardDataID,"card");
                be.SetBlessing(eo);
                be.ShowDialog();
                if (!string.IsNullOrEmpty(eo.name))
                {

                    listBox.IsSynchronizedWithCurrentItem = true;
                    NameMap.CardName.SetValue(select.cardDataID, eo.ToString());
                    reload();
                }
            }
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var card= listBox.SelectedItem as Card;
            if (null != card) { 
                labelName.Content = card.ToString();
                labelInfo.Content = card.GetInfo();
            }
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            var card = listBox.SelectedItem as Card;
            if (null != card)
            {
                var ncard = card.Clone() as Card;
                this.cards.Add(ncard);
                this.reload();
                GameObject.Instance.SetCards(this.cards);
            }
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            var card = listBox.SelectedItem as Card;
            if (null != card)
            {
                this.cards.Remove(card);
                this.reload();
                GameObject.Instance.SetCards(this.cards);
            }
        }
    }
}
